package com.rfid.api;

import com.rfid.spec.ISO18000p6CTagDataReport;
import com.rfid.spec.RFIDCOM_ISO180006C;
import com.rfid.spec.SpecAIPInvenParamISO180006C;
import com.rfid.spec.SpecInvenParamSet;
import com.rfid.spec.SpecTagAccess;
import com.rfid.spec.tagaccess.ISO180006C_TA_SpecLock;
import com.rfid.spec.tagaccess.ISO180006C_TA_SpecWrite;
import com.rfid.transport.BufferPack;

public class ISO18000p6CInterface {
    public static Object ISO18000p6C_CreateInvenParam(Object hInvenParamSpecList, byte AntennaID, byte Sel, byte Session, byte Target, byte Q) {
        SpecInvenParamSet pInvenParam = (SpecInvenParamSet) hInvenParamSpecList;
        if (pInvenParam == null) return null;
        SpecAIPInvenParamISO180006C iso180006cparam = SpecAIPInvenParamISO180006C.create();
        if (iso180006cparam == null) return null;
        iso180006cparam.m_enSelectParam = false;
        iso180006cparam.m_enInventoryRead = false;
        iso180006cparam.m_enMetaData = false;
        iso180006cparam.m_protoParam.m_selType = Sel;
        iso180006cparam.m_protoParam.m_session = Session;
        iso180006cparam.m_protoParam.m_Target = Target;
        iso180006cparam.m_protoParam.m_Q = Q;
        pInvenParam.AddAirProtocol(iso180006cparam);
        return iso180006cparam;
    }

    public static int ISO18000p6C_SetInvenSelectParam(Object hIso18000p6CInvenParam, byte target, byte action, byte memBank, long dwPointer, byte[] maskBits, int maskBitLen, byte truncate) {
        SpecAIPInvenParamISO180006C iso18000p6cPrm = (SpecAIPInvenParamISO180006C) hIso18000p6CInvenParam;
        if (iso18000p6cPrm == null) return -3;
        iso18000p6cPrm.m_enSelectParam = true;
        iso18000p6cPrm.m_selectParam.m_target = target;
        iso18000p6cPrm.m_selectParam.m_action = action;
        iso18000p6cPrm.m_selectParam.m_memBank = memBank;
        iso18000p6cPrm.m_selectParam.m_pointer = dwPointer;
        iso18000p6cPrm.m_selectParam.m_maskBits.WriteData(maskBits, 0, (maskBitLen + 7) / 8);
        iso18000p6cPrm.m_selectParam.m_maskBitsLen = maskBitLen;
        iso18000p6cPrm.m_selectParam.m_truncate = truncate;
        return 0;
    }

    public static int ISO18000p6C_SetInvenMetaDataFlags(Object hIso18000p6CInvenParam, long flags) {
        SpecAIPInvenParamISO180006C iso18000p6cPrm = (SpecAIPInvenParamISO180006C) hIso18000p6CInvenParam;
        if (iso18000p6cPrm == null) return -3;
        iso18000p6cPrm.m_enMetaData = true;
        iso18000p6cPrm.m_metaDataOption = flags;
        return 0;
    }

    public static int ISO18000p6C_SetInvenAccessPassword(Object hIso18000p6CInvenParam, long pwd) {
        SpecAIPInvenParamISO180006C iso18000p6cPrm = (SpecAIPInvenParamISO180006C) hIso18000p6CInvenParam;
        if (iso18000p6cPrm == null) return -3;
        iso18000p6cPrm.m_accessPwd = pwd;
        return 0;
    }

    public static int ISO18000p6C_SetInvenReadParam(Object hIso18000p6CInvenParam, byte MemBank, long WordPtr, byte WordCount) {
        SpecAIPInvenParamISO180006C iso18000p6cPrm = (SpecAIPInvenParamISO180006C) hIso18000p6CInvenParam;
        if (iso18000p6cPrm == null) return -3;
        iso18000p6cPrm.m_enInventoryRead = true;
        iso18000p6cPrm.m_readParam.m_MemBank = MemBank;
        iso18000p6cPrm.m_readParam.m_WordPtr = WordPtr;
        iso18000p6cPrm.m_readParam.m_WordCount = WordCount;
        return 0;
    }

    public static int ISO18000p6C_ParseTagReport(Object hTagReport, long[] aip_id, long[] tag_id, long[] ant_id, long[] metaFlags, byte[] tagdata, long[] tdLen) {
        ISO18000p6CTagDataReport ptdr = (ISO18000p6CTagDataReport) hTagReport;
        if (ptdr == null) return -3;
        if (ptdr.m_AIPType != 4L) return -4;
        if (aip_id != null) aip_id[0] = ptdr.m_AIPType;
        if (tag_id != null) tag_id[0] = ptdr.m_PICCType;
        if (ant_id != null) ant_id[0] = ptdr.m_AntID;
        BufferPack datas = new BufferPack(1024);
        if (ptdr.m_metaFlags == '\000') ptdr.m_metaFlags = (char) (ptdr.m_metaFlags | 0x1);
        if ((ptdr.m_metaFlags & 0x1) > '\000') {
            datas.WriteData((char) ptdr.m_bitsLen);
            datas.WriteData(ptdr.m_epc);
        }
        if ((ptdr.m_metaFlags & 0x2) > '\000') datas.WriteData((int) ptdr.m_timeStamp);
        if ((ptdr.m_metaFlags & 0x4) > '\000') datas.WriteData((int) ptdr.m_freq);
        if ((ptdr.m_metaFlags & 0x8) > '\000') datas.WriteData(ptdr.m_RSSI);
        if ((ptdr.m_metaFlags & 0x10) > '\000') datas.WriteData((byte) ptdr.m_readCnt);
        if ((ptdr.m_metaFlags & 0x20) > '\000') datas.WriteData(ptdr.m_tagData);
        int ncopy = datas.readable_length();
        if (ncopy > tdLen[0]) ncopy = (int) tdLen[0];
        if (tagdata != null) datas.ReadData(tagdata, ncopy);
        tdLen[0] = ncopy;
        if (metaFlags != null) metaFlags[0] = ptdr.m_metaFlags;
        return 0;
    }

    public static Object ISO18000p6C_CreateTAWrite(Object hIso18000p6CInvenParam, byte memBank, long wordPtr, long wordCnt, byte[] pdatas, long nSize) {
        ISO180006C_TA_SpecWrite p = ISO180006C_TA_SpecWrite.Create();
        if (p != null) {
            p.m_MemBank = memBank;
            p.m_WordPtr = wordPtr;
            p.m_WordCount = wordCnt;
            if (nSize > wordCnt * 2L) nSize = wordCnt * 2L;
            p.m_WordData.WriteData(pdatas, 0, (int) nSize);
            if (nSize < wordCnt * 2L) p.m_WordData.WriteData(null, 0, (int) (wordCnt * 2L - nSize));
            SpecAIPInvenParamISO180006C pAIP = (SpecAIPInvenParamISO180006C) hIso18000p6CInvenParam;
            if (pAIP != null) pAIP.m_accessSet.AddTagAccess(p);
        }
        return p;
    }

    public static int ISO18000p6C_CheckTAWriteResult(Object hTagReport) {
        ISO18000p6CTagDataReport ptdr = (ISO18000p6CTagDataReport) hTagReport;
        if (ptdr == null) return -3;
        if (ptdr.m_AIPType != 4L) return -4;
        SpecTagAccess p = ptdr.m_accessSet.GetTagAccessByOprCode(RFIDCOM_ISO180006C.CMD_WRITE);
        if (p == null) return -24;
        return p.m_ErrorCode;
    }

    public static Object ISO18000p6C_CreateTALock(Object hIso18000p6CInvenParam, char mask, char action) {
        ISO180006C_TA_SpecLock p = ISO180006C_TA_SpecLock.Create();
        if (p != null) {
            p.m_mask = mask;
            p.m_action = action;
            SpecAIPInvenParamISO180006C pAIP = (SpecAIPInvenParamISO180006C) hIso18000p6CInvenParam;
            if (pAIP != null) pAIP.m_accessSet.AddTagAccess(p);
        }
        return p;
    }

    public static int ISO18000p6C_CheckTALockResult(Object hTagReport) {
        ISO18000p6CTagDataReport ptdr = (ISO18000p6CTagDataReport) hTagReport;
        if (ptdr == null) return -3;
        if (ptdr.m_AIPType != 4L) return -4;
        SpecTagAccess p = ptdr.m_accessSet.GetTagAccessByOprCode(RFIDCOM_ISO180006C.CMD_LOCK);
        if (p == null) return -24;
        return p.m_ErrorCode;
    }
}
